package com.hurteng.stormplane.comm;

import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class CommClient {
    private DataInputStream socketInputStream;
    private DataOutputStream socketOutputStream;
    private PipedInputStream threadInputStream;
    private PipedOutputStream threadOutputStream;

    private String ip;
    private int port;

    private CommInitThread initThread;
    private CommSendThread sendThread;
    private CommRecvThread recvThread;

    public CommClient(String ip, int port) throws InterruptedException, IOException {
        this.ip = ip;
        this.port = port;
        threadInputStream = new PipedInputStream();
        threadOutputStream = new PipedOutputStream();
        this.init();
    }

    public CommClient() throws InterruptedException, IOException {
        this("192.168.31.160", 8000);
    }

    private void init() throws InterruptedException, IOException {
        initThread = new CommInitThread(ip, port);
        initThread.start();
        initThread.join();

        socketInputStream = new DataInputStream(initThread.getInputStream());
        socketOutputStream = new DataOutputStream(initThread.getOutputStream());

        sendThread = new CommSendThread(socketOutputStream);
        threadOutputStream.connect(sendThread.getThreadInputStream());
        sendThread.start();

        recvThread = new CommRecvThread(socketInputStream);
        threadInputStream.connect(recvThread.getThreadOutputStream());
        recvThread.start();
    }

    public void send(byte[] bytes) throws IOException {
        threadOutputStream.write(bytes);
    }

    public void send(byte[] bytes, int offset, int length) throws IOException {
        threadOutputStream.write(bytes, offset, length);
    }

    public int recv(byte[] bytes) throws IOException {
        return threadInputStream.read(bytes);
    }

    public int recv(byte[] bytes, int offset, int length) throws IOException {
        return threadInputStream.read(bytes, offset, length);
    }

    public CommObject command(CommObject cmd) throws IOException {
        CommObject ret = new CommObject();
        byte[] buffer = new byte[8192];

        send(cmd.toString().getBytes());
        int length = recv(buffer);
        ret = CommObject.parseCommObject(new String(buffer, 0, length));

        return ret;
    }

    public static void test() {
        Log.d("qinjinyu", "test begin");

        try {
            CommClient comm = new CommClient();
            CommObject cmd = new CommObject(-200, "arg0", "arg1", "arg2", "arg3");
            CommObject ret = comm.command(cmd);
            Log.d("qinjinyu", "cmd: " + cmd.toString());
            Log.d("qinjinyu", "ret: " + ret.toString());
        } catch (Exception e) {
            Log.d("qinjinyu", e.getMessage());
        }

        Log.d("qinjinyu", "test end");
    }
}
